home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17038 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.4 KB  |  173 lines

  1. Path: nuscc.nus.sg!satrajit
  2. From: satrajit@iscs.nus.sg (Satrajit Sujit Ghosh)
  3. Newsgroups: comp.lang.c++
  4. Subject: Is RTTI required if polymorphic constructor exists
  5. Date: 13 Apr 1996 06:20:24 GMT
  6. Organization: National University of Singapore
  7. Message-ID: <4knh38$lbl@nuscc.nus.sg>
  8. NNTP-Posting-Host: satrajit@sununx.iscs.nus.sg
  9. X-Newsreader: TIN [version 1.2 PL2]
  10.  
  11.  
  12. Hi,
  13.     The following is a piece of code for run-time object registry
  14. without the use of the RTTI library or templates. It should be pretty
  15. simple to decipher. It creates a polymorphic constructor, without
  16. specifying a case statement, using static initializers. 
  17.     Given this situation of object identification, is RTTI really necessary ?
  18.  
  19. Satra[jit]
  20. ------------------------------------------------------
  21. http://www.iscs.nus.sg/~satrajit
  22. Department of Information Systems and Computer Science
  23. National University of Singapore
  24. ======================================================
  25.  
  26.  
  27. /*
  28. /////////////////THE POLYMORPHIC CONSTRUCTOR///////////////////
  29. The following code segments demonstrate the creation of a polymorphic 
  30. constructor. 
  31. ///////////////////////////////////////////////////////////////
  32. */
  33.  
  34. #include <iostream.h>
  35. #include <string.h>
  36.  
  37. template<class T>
  38. class Clist{
  39. public:
  40.   Clist(){
  41.     i = 0;
  42.   };
  43.  
  44.   void insert(char *name,T *(*func)()){
  45.     for(int j=0;j<i;j++)
  46.       // prevent re-registration
  47.       if (!strcmp(nm_arr[j],name)){
  48.     return;
  49.       }
  50.  
  51.     nm_arr[i] = strdup(name);
  52.     fun[i] = func;
  53.     i++;
  54.   }
  55.  
  56.   T* get(char *name){
  57.     int j;
  58.     for(j=0;j<i;j++)
  59.       if (!strcmp(nm_arr[j],name)){
  60.     return (*fun[j])();
  61.       }
  62.     return NULL;
  63.   };
  64.  
  65.   // currently can support 5 derived classes
  66.   char *nm_arr[5];
  67.   T *(* fun[5])();
  68.   int i;
  69. };
  70.  
  71.  
  72.  
  73. class CShape{
  74. public:
  75.   virtual void print(){
  76.     cout << "reg" << endl;
  77.   };
  78.  
  79.   static CShape * mk_subclass(char *str){
  80.     return reg.get(str);
  81.   }
  82.  
  83. protected:
  84.   static int reg_sub(char *name,CShape *(*func)()){
  85.     reg.insert(name,func);
  86.     return 0;
  87.   };
  88.  
  89. private:  
  90.   static Clist<CShape> reg;
  91.  
  92. };
  93.  
  94. Clist<CShape> CShape::reg;
  95.  
  96. class CCircle:public virtual CShape{
  97. public:
  98.   virtual void print(){
  99.     cout <<"CCircle";
  100.   };
  101.  
  102.   static CShape* int_new(){
  103.     CShape *p = new CCircle;
  104.     return p;
  105.   };
  106. private:
  107.   static int i;
  108. };
  109.  
  110. int CCircle::i = reg_sub("CCircle",&CCircle::int_new);
  111.  
  112. class CRect:public virtual CShape{
  113. public:
  114.  
  115.   virtual void print(){
  116.     cout <<"CRect";
  117.   };
  118.  
  119.   static CShape* int_new(){
  120.     CShape *p = new CRect;
  121.     return p;
  122.   };
  123.  
  124. private:
  125.   static int i;
  126. };
  127.  
  128. int CRect::i =  reg_sub("CRect",&CRect::int_new);
  129.  
  130. class CSquare:public virtual CRect{
  131. public:
  132.  
  133.   virtual void print(){
  134.     cout <<"CSquare";
  135.   };
  136.  
  137.   static CShape* int_new(){
  138.     CShape *p = new CSquare;
  139.     return p;
  140.   };
  141.  
  142. private:
  143.   static int i;
  144. };
  145.  
  146. int CSquare::i =  reg_sub("CSquare",&CSquare::int_new);
  147.  
  148.  
  149. int 
  150. main(){
  151.   CShape *fg = CShape::mk_subclass("CCircle");
  152.   cout << "Testing constructed CCircle [";
  153.   fg->print();
  154.   cout << "]\n";
  155.   CShape *fg1 = CShape::mk_subclass("CRect");
  156.   cout << "Testing constructed CRect [";
  157.   fg1->print();
  158.   cout << "]\n";
  159.   CShape *fg2 = CShape::mk_subclass("CSquare");
  160.   cout << "Testing constructed CSquare [";
  161.   fg2->print();
  162.   cout << "]\n";
  163.   return 0;
  164. }
  165.  
  166. --
  167. Satra[jit]
  168. ------------------------------------------------------
  169. http://www.iscs.nus.sg/~satrajit
  170. Department of Information Systems and Computer Science
  171. National University of Singapore
  172. ======================================================
  173.